<template>
  <div>
    <el-card class="search-card">
      <BasicForm
        ref="formData"
        showColon
        defaultExpandStatus
        formItemWidth="130px"
        :formColumns="formItems"
        :dataSource="formData"
        :handleSearchForm="handleQueryForm"
        @getFormData="getFormData"
      />
    </el-card>
    <el-card class="table-card">
      <SkusTable
        showActionCol
        showPagination
        :tableHeight="480"
        :actionColWidth="200"
        :dataSource="TableInfo.list"
        :columns="TableInfo.columns"
        :loading="TableInfo.loading"
        :pageSizes="pageSizes"
        :pagination="pagination"
        :total="total"
        :actionRender="renderTableBtnAction"
        @pageChange="handlePageChange"
        @pageSizeSelect="handlePageSizeChange"
      />
    </el-card>
  </div>
</template>
<script>
import BasicForm from '@/components/BasicForm';
import SkusTable from '@/components/skusTable';
import BtnRender from '@/components/JSXRender/BtnRender';
import skusPage from '@/mixins/commodity/skusPage';
import { FORM_ITEMS, TABLE_COLUMNS } from './constants/list';
import { WORKFLOW_STATUS_ENUMS } from './constants/config';
import { getInitFormData, TableInfo } from '@/utils/getUtils';
import { setAuthBtnId } from '@/utils/bizUtils';
import { getTableAction } from '@/utils/getBtnsRender';
import { recordSearchService } from '@/api/commodity/temporaryPurchasingGoods/latentProductRecord';
import { queryOAUrlService } from '@/api/public';
export default {
  mixins: [skusPage],
  components: {
    BasicForm,
    SkusTable,
    BtnRender,
  },
  data () {
    return {
      formItems: FORM_ITEMS,
      formData: getInitFormData(FORM_ITEMS),
      TableInfo: new TableInfo({ columns: TABLE_COLUMNS }),
    }
  },
  created () {
    this.updateFormDataBySourcing()
    this.getTableData()
  },
  methods: {
    // 渲染表格操作栏按钮
    renderTableBtnAction (h, { row }) {
      const viewBtn = { label: '查看', method: this.handleView }
      const reviewBtn = { label: '审核', method: this.handleReview, hasAuth: true }
      const reviewRecordBtn = { label: '审批记录', method: this.handleReviewRecord }
      const BtnMaps = {
        [WORKFLOW_STATUS_ENUMS.ToSubmit]: [viewBtn],
        [WORKFLOW_STATUS_ENUMS.ToSubmitOA]: [viewBtn],
        [WORKFLOW_STATUS_ENUMS.ToReview]: [viewBtn, reviewBtn],
        [WORKFLOW_STATUS_ENUMS.ReviewPass]: [viewBtn, reviewRecordBtn],
        [WORKFLOW_STATUS_ENUMS.ReviewReject]: [viewBtn, reviewRecordBtn],
        [WORKFLOW_STATUS_ENUMS.PartApprove]: [viewBtn, reviewRecordBtn],
      }
      const actionList = BtnMaps[row.workflowStatus] ?? []
      return getTableAction({ h, row, actionList, showBtn: this.showBtn })
    },
    // 获取表格数据
    async getTableData () {
      setAuthBtnId('查询')
      const data = { ...this.formData, ...this.paginationParams }
      const res = await recordSearchService(data, this.TableInfo.setLoading)
      this.TableInfo.setList(res?.retData?.results ?? [])
      this.setTotalNum(res?.retData?.totalNum ?? 0)
    },
    // 查看
    handleView (row) {
      const { href } = this.$router.resolve({
        name: 'latentProductRecordRecordView',
        query: { recordNo: row.recordNo, hasAuth: true } // 列表进入进行权限校验。其他地方跳入不校验
      })
      window.open(href, '_blank')
    },
    // 审核
    handleReview (row) {
      setAuthBtnId('审核')
      this.redirectOA(row.workflowRequestId)
    },
    // 审批记录
    handleReviewRecord (row) {
      this.redirectOA(row.workflowRequestId)
    },
    // 跳转OA
    async redirectOA (oaRequestId) {
      const data = { oaRequestId }
      const res = await queryOAUrlService(data, this.TableInfo.setLoading)
      if (res?.retData) window.open(res.retData)
    },
    // 寻源下推查询条件回写
    updateFormDataBySourcing () {
      const { orderInquiryNo } = this.$route?.query ?? {}
      if (!orderInquiryNo) return
      this.formData.orderInquiryNo = orderInquiryNo
      this.formData.workflowStatus = WORKFLOW_STATUS_ENUMS.ToReview
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .table-action {
  margin-right: 0;
}
</style>
